using System;
using System.Collections.Generic;
using System.Linq;
using Jarvis.Server.Domain;
using Jarvis.Server.Persistence;

namespace Jarvis.Server.Providers
{
    public class StatsProvider : IStatsProvider
    {
        private readonly ILocationStatsRepository _locationStats;

        public StatsProvider(ILocationStatsRepository locationStats)
        {
            _locationStats = locationStats;
        }

        #region Implementation of IStatsProvider

        public IList<LocationStats> GetAllStats()
        {
            IList<LocationStats> retval;

            using(_locationStats.OpenSession())
            {
                retval = _locationStats.All().ToList();
            }

            return retval;
        }

        public LocationStats StatsForLocation(Location location)
        {
            LocationStats retval;

            using (_locationStats.OpenSession())
            {
                retval = (from l in _locationStats.All()
                          where l.Location.Id == location.Id
                          select l).First();
            }
            return retval;
        }

        public void InsertOrUpdateLocationStats(DateTime now, Location loc, LocationStatsEntry.EntryKind entryKind)
        {
            using (var session = _locationStats.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {

                    var persistedStat = (from l in _locationStats.All()
                                         where l.Location.Id == loc.Id
                                         select l).FirstOrDefault();

                    if (persistedStat != null)
                    {
                        persistedStat.Entries.Add(new LocationStatsEntry {Date = now, Kind = entryKind});




                        _locationStats.Update(persistedStat);


                    }
                    else
                    {
                        persistedStat = new LocationStats
                                            {
                                                Entries = new List<LocationStatsEntry>
                                                              {
                                                                  new LocationStatsEntry
                                                                      {
                                                                          Date = now,
                                                                          Kind = entryKind
                                                                      }
                                                              },
                                                Location = loc
                                            };

                        _locationStats.Add(persistedStat);

                    }
                    tx.Commit();
                }
            }
        }

        #endregion
    }
}